﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CommonLibrary.DataModel.WorkFlow;
using CommonLibrary.DB;

namespace ServiceApplication.Dao.WorkFlow
{
    public class WfTemplateDao
    {
        #region 基本
        /// <summary>
        /// 返回刚插入的主键
        /// </summary>
        /// <param name="wfModuleEntity"></param>
        /// <returns></returns>
        public int AddEntity(WfTemplateEntity wfTemplateEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftemplate_name", wfTemplateEntity.wftemplate_name));
            parameterList.Add(new SqlParameter("@wfdefinition", wfTemplateEntity.wfdefinition));
            parameterList.Add(new SqlParameter("@wfrule", wfTemplateEntity.wfrule));
            parameterList.Add(new SqlParameter("@wflayout", wfTemplateEntity.wflayout));
            parameterList.Add(new SqlParameter("@creater_id", wfTemplateEntity.creater_id));
            parameterList.Add(new SqlParameter("@creater_name", wfTemplateEntity.creater_name));
            parameterList.Add(new SqlParameter("@creater_time", wfTemplateEntity.creater_time));
            parameterList.Add(new SqlParameter("@imgpath", wfTemplateEntity.imgpath));
            string sql = " INSERT INTO wf_template(wftemplate_name,wfdefinition,wfrule,wflayout,creater_id,creater_name,creater_time,imgpath )VALUES " +
                " (@wftemplate_name,@wfdefinition,@wfrule,@wflayout,@creater_id,@creater_name,@creater_time,@imgpath);select @@identity";
            string id = DBExecuteUtil.getUniqueResult(sql, parameterList.ToArray());
            return int.Parse(id);
        }

        public void DleteEntity(int wftemplate_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftemplate_id", wftemplate_id));
            string sql = "delete from wf_template where wftemplate_id=@wftemplate_id ";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public void UpdateEntity(WfTemplateEntity wfTemplateEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftemplate_id", wfTemplateEntity.wftemplate_id));
            parameterList.Add(new SqlParameter("@wftemplate_name", wfTemplateEntity.wftemplate_name));
            parameterList.Add(new SqlParameter("@wfdefinition", wfTemplateEntity.wfdefinition));
            parameterList.Add(new SqlParameter("@wfrule", wfTemplateEntity.wfrule));
            parameterList.Add(new SqlParameter("@wflayout", wfTemplateEntity.wflayout));
            string sql = "update wf_template set wftemplate_name=@wftemplate_name,wfdefinition=@wfdefinition,wfrule=@wfrule,wflayout=@wflayout," +
                "creater_id=@creater_id,creater_name=@creater_name,creater_time=@creater_time,imgpath=@imgpath " +
                " where wftemplate_id=@wftemplate_id) ";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public WfTemplateEntity QueryEntity(int wftemplate_id)
        {
            WfTemplateEntity wfTemplateEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftemplate_id", wftemplate_id));
            string sql = "select wftemplate_id,wftemplate_name,wfdefinition,wfrule,wflayout,creater_id,creater_name,creater_time,imgpath from wf_template where wftemplate_id=@wftemplate_id ";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (null == result) return null;

            wfTemplateEntity = new WfTemplateEntity();
            wfTemplateEntity.wftemplate_id = int.Parse(result[0][0]);
            wfTemplateEntity.wftemplate_name = result[0][1];
            wfTemplateEntity.wfdefinition = result[0][2];
            wfTemplateEntity.wfrule = result[0][3];
            wfTemplateEntity.wflayout = result[0][4];
            wfTemplateEntity.creater_id = result[0][5];
            wfTemplateEntity.creater_name = result[0][6];
            wfTemplateEntity.creater_time = result[0][7];
            wfTemplateEntity.imgpath = result[0][8];
            return wfTemplateEntity;
        }



        public DataTable QueryAll()
        {
            string sql = "select wftemplate_id 编码,wftemplate_name 名称,wfdefinition 定义,wfrule 规则,wflayout 外表,creater_name 创建人,creater_id 创建人编码,creater_time 创建时间,imgpath 流程图 from wf_template ";
            DataTable dt = DBExecuteUtil.querySqlTable(sql);
            return dt;
        }

        #endregion
    }
}
